#include "MessageQueue.h"
#include "Game.h"
#include <assert.h>
bool MessageQueue::GMLessThan::operator ()(GameMessage* gm1, GameMessage* gm2)
{
	return gm1->GetTimestamp() < gm2->GetTimestamp();
}

void MessageQueue::AddToQueue(GameMessage* gm)
{
	assert(gm);

	Lock lock(&m_Mutex);
	m_Queue.push(gm);
}

void MessageQueue::Update()
{
	Lock lock(&m_Mutex);
	while(true)
	{
		if(m_Queue.empty())
		{
			return;
		}
		float currentTime = TheGame::Instance()->GetTime();
		GameMessage* gm = m_Queue.top();
		if(gm->GetTimestamp() <= currentTime)
		{
			m_Queue.pop();
			gm->Execute();
		}
		else
		{
			return;
		}
	}
}